package com.woniuxy.joylifemarket.controller;

import com.woniuxy.common.entity.MarketCollect;
import com.woniuxy.joylifemarket.service.MarketCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CollectController {
    @Autowired
    MarketCollectService marketCollectService;

    //查询登录用户全部收藏商品
    @RequestMapping("/searchicollect")
    public Object iCollect(String loginuserid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketCollect> allCollect = marketCollectService.findAllCollectByUserId(loginuserid);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data",allCollect);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //查询登录用户是否收藏商品的状态
    @RequestMapping("/initcollectstatus")
    public Object collectStatus(String commodityid,String loginuserid) {
//        System.out.println("commodityid"+commodityid+"loginuserid"+loginuserid);
        Map<String, Object> result = new HashMap<>();
        try {
            Boolean collectStatus = marketCollectService.findCollectStatus(commodityid, loginuserid);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data",collectStatus);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }

    //更改登录用户收藏该商品的状态
    @RequestMapping("/changecollectstatus")
    public Object changeCollectStatus(String commodityid,String loginuserid,String collectstatus) {
        //System.out.println("commodityid:"+commodityid+",loginuserid:"+loginuserid+",collectstatus:"+collectstatus);
        Map<String, Object> result = new HashMap<>();
        try {
            //判断状态，如果没有收藏，添加收藏
            if (collectstatus.equals("false")) {
                int i = marketCollectService.insertCollect(commodityid, loginuserid);
                if (i!=0) {
                    result.put("code", 200);
                    result.put("msg", "success");
                    result.put("data",true);
                    return result;
                }else{
                    result.put("code", 500);
                    result.put("msg", "default");
                    return result;
                }
            //已经收藏过，则执行删除收藏操作
            }else if(collectstatus.equals("true")){
                int i = marketCollectService.deleteCollet(commodityid, loginuserid);
                if (i!=0) {
                    result.put("code", 200);
                    result.put("msg", "success");
                    result.put("data",false);
                    return result;
                }else{
                    result.put("code", 500);
                    result.put("msg", "default");
                    return result;
                }
            }
            result.put("code", 500);
            result.put("msg", "default");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "default");
            e.printStackTrace();
            return result;
        }
    }
}
